<?php

class LoginModel extends CI_Model {

    function __construct() {
        parent::__construct();
    }


    private $table = 'tbl_user';
    private $tbl_role = 'tbl_role';
    public $id = 0;
    public $role = '';
    public $role_id = 0;
    public $firstname = '';
    public $lastname = '';
    public $error = '';

    public function check_credentials($username, $password) {
        $this->db->select('UserID, FirstName, LastName, tbl_role.RoleID, RoleName')
                ->join('tbl_role', 'tbl_role.RoleID = ' . $this->table . '.RoleID')
                ->where('Username', $username)
                ->where('Password', md5($password))
                ->where($this->table . '.Enable', true)
                ->limit(1);

        $query = $this->db->get($this->table);

        if ($query->num_rows() > 0) {
            $row = $query->row();
            $this->id = $row->UserID;
            $this->firstname = $row->FirstName;
            $this->lastname = $row->LastName;
            $this->role_id = $row->RoleID;
            $this->role = $row->RoleName;

            return true;
        }

        return false;
    }
    public function error() {
        return $this->error;
    }

}